package com.tanhua.appserver.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.appserver.hanlder.UserHolder;
import com.tanhua.appserver.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/users")
public class UsersController {


    @Autowired
    private UserInfoService userInfoService;


    /**
     * 用户查询个人资料的时候进入这个接口
     * @param userId
     * @return
     */
    @GetMapping
    public ResponseEntity findUserInfoById(Long userId){
        if (userId==null){
            userId=UserHolder.getUserId();
        }
        UserInfoVo userInfo=userInfoService.findUserInfoById(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 修改用户个人信息
     *
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){

        userInfo.setId(UserHolder.getUserId());

        userInfoService.updateUserInfo(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改用户头像
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(@RequestBody MultipartFile headPhoto) throws IOException {

        userInfoService.updateHead(headPhoto,UserHolder.getUserId());

        return ResponseEntity.ok(null);
    }


}
